/**
 * 获取cookie
 * @param key
 * @returns {*}
 */
export let getCookie = (key) => {
    if (document.cookie) {
        if (document.cookie.indexOf(encodeURIComponent(key)) !== -1) {
            return decodeURIComponent(document.cookie.split("; ").filter(function (item) {
                return item.indexOf(encodeURIComponent(key)) === 0;
            })[0].split("=")[1]);
        } else {
            return "";
        }
    } else {
        return "";
    }
}

/**
 * 设置cookie
 * @param key
 * @param value
 * @param days
 * @param path
 */
export let setCookie = (key, value, days, path) => {
    path = path || "/";
    if (days) {
        var date = new Date();
        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(0);
        date.setDate(date.getDate() + days);
        document.cookie = encodeURIComponent(key) + "=" + encodeURIComponent(value) + ";expires=" + date + ";path=" + path;
    } else {
        document.cookie = encodeURIComponent(key) + "=" + encodeURIComponent(value) + ";path=" + path;
    }
}
